package org.nimi317.web_gis.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import org.nimi317.web_gis.context.SecurityContextHolder;
import org.nimi317.web_gis.entity.R;
import org.nimi317.web_gis.service.ICurrentService;
import org.nimi317.web_gis.service.IStationQualityService;
import org.nimi317.web_gis.service.ITableRangeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author thunderobot
 */
@RestController
@SaCheckRole("admin")
@RequestMapping("/table")
public class TableController {

    private final ITableRangeService tableRangeService;

    private final ICurrentService currentService;

    private final IStationQualityService stationQualityService;

    public TableController(ITableRangeService tableRangeService, ICurrentService currentService, IStationQualityService stationQualityService) {
        this.tableRangeService = tableRangeService;
        this.currentService = currentService;
        this.stationQualityService = stationQualityService;
    }


    @GetMapping("range")
    public R<?> get() {
        return R.ok(tableRangeService.list());
    }

    @GetMapping("current")
    public R<?> getCurrent() {
        return R.ok(currentService.getCurrentDB());
    }

    @GetMapping("add/{table}")
    public R<?> add(@PathVariable String table) {
        tableRangeService.createTableAndAddRange(table);
        return R.ok();
    }

    @GetMapping("count/{table}")
    public R<?> count(@PathVariable String table) {
        SecurityContextHolder.setTableName(table);
        long count = stationQualityService.count();
        SecurityContextHolder.remove();
        return R.ok(count);
    }
}
